<?php
	class AdditionalCategory extends AppModel
	{
		var $name = "AdditionalCategory";
		
		var $fieldProperties = array(
			'list_category_id' => array('type'=>"select",'group'=>"child_add",'label'=>false),
		);
		var $belongsTo = array('Listing');
        //save qurey for list_category_id. Lazy call for categories options.
		function getFieldProperties($field, $key=null)
		{	
			if($field == "list_category_id" && ($key == null || $key == "options") && !isset($this->fieldProperties['list_category_id']['options']))
			{
				$this->fieldProperties['list_category_id']['options'] = cri('ListCategory')->getChildrenOptions(null);
			}
			return parent::getFieldProperties($field,$key);
		}
		function beforeSave($options = array())
		{
			$listingCategoryId = null;
			if(isset($this->data[$this->alias]['listing_id']) && $this->data[$this->alias]['listing_id'])
			{
				$this->data[$this->alias]['status'] = 1;
        		cri("ListCategory")->clearCaheForListing($this->data[$this->alias]['list_category_id']);
				GigaModelCache::delete($this->alias,$this->data[$this->alias]['listing_id'],"categories_list");
				
				$listingCategoryId = $this->Listing->field('list_category_id',array('Listing.id'=>$this->data[$this->alias]['listing_id']));
			}
			if($this->data[$this->alias]['list_category_id'] === null)
			{
					return false;
			}
			return parent::beforeSave($options);
		}
		function beforeChildrenSave($parentFieldData,$model)
		{ 
			$categories = array();
			foreach($parentFieldData as $key => $data)
			{
				//don't allow empty fields or duplicate entries.
				if(!$data['list_category_id'] || in_array($data['list_category_id'],$categories))
				{
					unset($parentFieldData[$key]);
				}else
				{
					$categories[] = $data['list_category_id'];
					unset($parentFieldData[$key]['id']);
				}	
			}
			//@todo fix me. and find reason for doing this before commiting.
			if(count($parentFieldData) == 0)
			{
			   //$parentFieldData [] = array('list_category_id'=>0);
			}
			return $parentFieldData;
		}
		function getCategoriesList($listingId)
		{
			$categories = $this->find('list',array('conditions'=>array('listing_id'=>$listingId,'status'=>1),'fields'=>array('list_category_id')));
			if(!$categories)
			{
				$categories = array();
			}
			return $categories;
		}
		function getAdditionalCategories($listingId)
		{
			$ListCategory = cri('ListCategory');
			$categorieList = $this->getCategoriesList($listingId);
			$categories = array();
			foreach($categorieList as $categoryId)
			{
				$categories[$categoryId] = $ListCategory->getCategoryInfo($categoryId);
			}
			return $categories;
		}
		function getCategoryListings($categoryIds,$limit = 10000)
		{
          //@todo listing_count should also consider additional category
			$listings =  $this->find('all',array('fields'=>array('DISTINCT(listing_id)'),'conditions'=>array('list_category_id'=>$categoryIds,'status'=>1),'recursive'=>-1,'limit'=>$limit));
            $listings = Set::extract($listings,'{n}.AdditionalCategory.listing_id');
            return $listings;
    }
		
     function getCategoryListingCount($categoryIds)
     {
          $count = $this->find("first",array("conditions"=>array('AdditionalCategory.list_category_id'=>$categoryIds,'AdditionalCategory.status'=>1,'Listing.status'=>Listing::PUBLISH, 'NOT'=>array('Listing.list_category_id'=>$categoryIds)),'fields'=>array('count(DISTINCT AdditionalCategory.id) as ct'),'recursive'=>0));
          $listingCount = 0;
          if($count && isset($count[0]['ct']))
          {
              $listingCount = $count[0]['ct'];
          }
          return $listingCount;
    }
		function activateAllCategories($listingId)
		{
			$this->recursive = -1;
			$this->updateAll(array('status'=>1),array('listing_id'=>$listingId));
			GigaModelCache::delete($this->alias,$listingId,"categories_list");
		}

		function deactivateAllCategories($listingId)
		{
			$this->recursive = -1;
			$this->updateAll(array('status'=>0),array('listing_id'=>$listingId));
			GigaModelCache::delete($this->alias,$listingId,"categories_list");
		}
	}
?>